var get_blobs = function(memtile,rect){
  
  var blobs = RectManager(1); //allow overlap

  var seek_blob_at = function(x,y){
    if(blobs.rectAt(x,y) != -1)
      return 1;
    var myblob = [];    
    visit(memtile.pixels,myblob,x,y);
    return myblob;  
  }

  for(var y in memtile.pixels){
    for(var x in memtile.pixels[y]){
      y = Number(y);
      x = Number(x);
      if(!is_nonblob_colored(memtile.pixels[y][x])){
        var rect = rectFromCoordSet(seek_blob_at(x,y));
        if(rect !=1)
          blobs.makeRect(rect.xD,rect.yD,rect.xS,rect.yS);
      }
    }
  } 
  return blobs;
}

var rectFromCoordSet = function(list){
  if(list==1)
    return 1;
  var maxx = 0;
  var maxy = 0;
  var minx = list[0][0];
  var miny = list[0][1];
  for(i in list){
    if(list[i][0]>maxx)
      maxx=list[i][0];
    if(list[i][1]>maxy)
      maxy=list[i][1];
    if(minx>list[i][0])
      minx = list[i][0];
    if(miny>list[i][1])
      miny = list[i][1];
  }
  var rect = {};
  rect.xD = minx;
  rect.yD = miny;
  rect.xS = maxx - minx +1 ;
  rect.yS = maxy - miny +1;

  return rect;
  

}
var visit = function(pixels,myblob,x,y){

  if(coordsInList(x,y,myblob) != -1)
    return 1;
  myblob.push([x,y]);
  var neighbors = get_neighbors(x,y,pixels);
  if(neighbors[0] == undefined)
    return 0;
  for(var i in neighbors){
    visit(pixels,myblob,neighbors[i][0],neighbors[i][1]);
  }  
}


//NONBLOBCOLOR = {r:0,g:0,b:0,a:255};
NONBLOBCOLOR  = {r:255,g:255,b:255,a:255};
var is_nonblob_colored = function(color){
  if(color == undefined)
    return 1;
  if(color.a == 0)
    return 1;
  if(compareColors(color,NONBLOBCOLOR) )
    return 0;
  return 1;
}

var get_neighbors = function(x,y,pixels){
  var neighbors = [];
  x = Number(x);
  y = Number(y);
  //Dumb and simple is better than dumb and complex.
  if(pixels[y+1] !=undefined){

  if(is_nonblob_colored(pixels[y+1][x-1]) == 0)
    neighbors.push([x-1,y+1]);
  if(is_nonblob_colored(pixels[y+1][x]) == 0)
    neighbors.push([x,y+1]);
  if(is_nonblob_colored(pixels[y+1][x+1]) == 0)
    neighbors.push([x+1,y+1]);
  }
  if(is_nonblob_colored(pixels[y][x-1]) == 0)
    neighbors.push([x-1,y]);
  if(is_nonblob_colored(pixels[y][x+1]) == 0)
    neighbors.push([x+1,y]);
  if(pixels[y-1] !=undefined){
  if(is_nonblob_colored(pixels[y-1][x-1]) == 0)
    neighbors.push([x-1,y-1]);
  if(is_nonblob_colored(pixels[y-1][x]) == 0)
    neighbors.push([x,y-1]);
  if(is_nonblob_colored(pixels[y-1][x+1]) == 0)
    neighbors.push([x+1,y-1]);
  }
  return neighbors;
  
}
var  coordsInList = function(x,y,list){
  for(var i in list){
    if(list[i][0] == x && list[i][1] == y)
      return 0;
  }
  return -1;
}

//unit test
function test(){
m = Mosaic("canvases",{tileSize:4,bg:{r:255,g:255,b:255,a:255}});
i = ImageLoader("font2");
mem = MemTile(m,0,0,i.width,i.height);
i.sprayImage(mem);
var blobs = get_blobs(mem);
var selector = Selector(mem);
for(rect in blobs.rects){
  selector.select(blobs.rects[rect]);
}
}

var max_dimensions = function(rects){
  var maxwidth = 0;
  var maxheight = 0;
  for(var i in rects){
    if(rects[i].xD>maxwidth){
      maxwidth = rects[i].xD;
      maxheight = rects[i].yD;
    }
  }
  return [maxwidth,maxheight]

}
var median_size = function(rects){
  var widths = [];
  var heights = [];
  for(var i in rects){
    widths.push(Number(rects[i].xS));
    heights.push(Number(rects[i].yS));
  }
  widths.sort(numcompare);
  heights.sort(numcompare);
  middle = Math.floor(widths.length/2);
  return [widths[middle],heights[middle]];
}
var numcompare = function(a,b){
  return a-b;
}
//
